<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计分析 - 家庭工具管理</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" href="styles.css">
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
</head>
<body class="bg-gray-50 h-full">
    <!-- 页面内容 -->
    <div class="h-full flex flex-col">
        <!-- 顶部导航栏 -->
        <div class="bg-white shadow-sm">
            <div class="flex items-center justify-between px-4 py-3">
                <button onclick="window.parent.navigateTo('home.html')" class="p-1 text-gray-500">
                    <i class="fa fa-arrow-left"></i>
                </button>
                <h1 class="text-lg font-medium text-dark">统计分析</h1>
                <button class="p-1 text-gray-500">
                    <i class="fa fa-calendar"></i>
                </button>
            </div>
        </div>
        
        <!-- 数据概览 -->
        <div class="bg-white p-4 border-b border-gray-100">
            <div class="grid grid-cols-3 gap-3">
                <div class="bg-gray-50 rounded-lg p-3 text-center">
                    <div class="text-2xl font-bold text-primary">26</div>
                    <div class="text-xs text-gray-500 mt-1">工具总数</div>
                </div>
                <div class="bg-gray-50 rounded-lg p-3 text-center">
                    <div class="text-2xl font-bold text-blue-500">¥8,942</div>
                    <div class="text-xs text-gray-500 mt-1">总支出</div>
                </div>
                <div class="bg-gray-50 rounded-lg p-3 text-center">
                    <div class="text-2xl font-bold text-amber-500">78</div>
                    <div class="text-xs text-gray-500 mt-1">本月使用次数</div>
                </div>
            </div>
        </div>
        
        <!-- 统计图表区域 -->
        <div class="flex-1 overflow-y-auto">
            <div class="bg-white p-4 mb-3">
                <h2 class="text-base font-medium text-dark mb-3">工具类别分布</h2>
                <div class="w-full h-64">
                    <canvas id="categoryChart"></canvas>
                </div>
            </div>
            
            <div class="bg-white p-4 mb-3">
                <h2 class="text-base font-medium text-dark mb-3">工具使用频率</h2>
                <div class="w-full h-64">
                    <canvas id="usageChart"></canvas>
                </div>
            </div>
            
            <div class="bg-white p-4 mb-3">
                <h2 class="text-base font-medium text-dark mb-3">按年份支出统计</h2>
                <div class="w-full h-64">
                    <canvas id="expenseChart"></canvas>
                </div>
            </div>
            
            <div class="bg-white p-4">
                <h2 class="text-base font-medium text-dark mb-3">工具状态分析</h2>
                <div class="grid grid-cols-2 gap-3">
                    <div class="bg-gray-50 rounded-lg p-3">
                        <div class="flex justify-between items-center mb-2">
                            <span class="text-sm text-gray-600">在用</span>
                            <span class="text-sm font-medium">18</span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2">
                            <div class="bg-green-500 h-2 rounded-full" style="width: 69%"></div>
                        </div>
                    </div>
                    
                    <div class="bg-gray-50 rounded-lg p-3">
                        <div class="flex justify-between items-center mb-2">
                            <span class="text-sm text-gray-600">闲置</span>
                            <span class="text-sm font-medium">5</span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2">
                            <div class="bg-blue-500 h-2 rounded-full" style="width: 19%"></div>
                        </div>
                    </div>
                    
                    <div class="bg-gray-50 rounded-lg p-3">
                        <div class="flex justify-between items-center mb-2">
                            <span class="text-sm text-gray-600">已借出</span>
                            <span class="text-sm font-medium">3</span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2">
                            <div class="bg-purple-500 h-2 rounded-full" style="width: 12%"></div>
                        </div>
                    </div>
                    
                    <div class="bg-gray-50 rounded-lg p-3">
                        <div class="flex justify-between items-center mb-2">
                            <span class="text-sm text-gray-600">损坏</span>
                            <span class="text-sm font-medium">0</span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2">
                            <div class="bg-red-500 h-2 rounded-full" style="width: 0%"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // 页面加载完成后的处理
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化图表
            initCharts();
            
            // 为日历按钮添加点击事件
            document.querySelector('.fa-calendar').parentElement.addEventListener('click', function() {
                // 打开日期选择器
                alert('打开日期选择器');
            });
        });
        
        // 初始化所有图表
        function initCharts() {
            // 工具类别分布图表
            const categoryCtx = document.getElementById('categoryChart').getContext('2d');
            new Chart(categoryCtx, {
                type: 'doughnut',
                data: {
                    labels: ['电动工具', '手动工具', '园艺工具', '测量工具', '厨房工具'],
                    datasets: [{
                        data: [8, 7, 4, 3, 4],
                        backgroundColor: [
                            '#16a34a',
                            '#3b82f6',
                            '#8b5cf6',
                            '#f59e0b',
                            '#ec4899'
                        ],
                        borderWidth: 0
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            position: 'bottom',
                            labels: {
                                usePointStyle: true,
                                padding: 15,
                                font: {
                                    size: 12
                                }
                            }
                        }
                    }
                }
            });
            
            // 工具使用频率图表
            const usageCtx = document.getElementById('usageChart').getContext('2d');
            new Chart(usageCtx, {
                type: 'bar',
                data: {
                    labels: ['电动螺丝刀', '电钻', '扳手套装', '螺丝刀套装', '园艺修剪机', '激光测距仪'],
                    datasets: [{
                        label: '使用次数',
                        data: [24, 18, 12, 15, 8, 11],
                        backgroundColor: '#16a34a',
                        borderRadius: 4
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            grid: {
                                display: true,
                                color: '#f1f5f9'
                            },
                            ticks: {
                                font: {
                                    size: 10
                                }
                            }
                        },
                        x: {
                            grid: {
                                display: false
                            },
                            ticks: {
                                font: {
                                    size: 10
                                },
                                maxRotation: 45,
                                minRotation: 45
                            }
                        }
                    }
                }
            });
            
            // 按年份支出统计图表
            const expenseCtx = document.getElementById('expenseChart').getContext('2d');
            new Chart(expenseCtx, {
                type: 'line',
                data: {
                    labels: ['2020', '2021', '2022', '2023', '2024'],
                    datasets: [{
                        label: '支出金额（元）',
                        data: [1200, 2800, 3200, 1500, 242],
                        borderColor: '#3b82f6',
                        backgroundColor: 'rgba(59, 130, 246, 0.1)',
                        tension: 0.4,
                        fill: true
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            grid: {
                                display: true,
                                color: '#f1f5f9'
                            },
                            ticks: {
                                font: {
                                    size: 10
                                }
                            }
                        },
                        x: {
                            grid: {
                                display: false
                            },
                            ticks: {
                                font: {
                                    size: 10
                                }
                            }
                        }
                    }
                }
            });
        }
    </script>
</body>
</html>